package com.lovo.service.serviceImpl;

import com.lovo.bean.RoleThrone;
import com.lovo.dao.RoleThroneMapper;
import com.lovo.service.RoleThroneService;
import com.lovo.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class RoleThroneServiceImpl implements RoleThroneService {


    @Override
    public boolean addRoleThrone(String value, String roleId) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        RoleThroneMapper mapper = sqlSession.getMapper(RoleThroneMapper.class);
        String [] strs = value.split(",");
        int n = 0;
        for (int i = 0; i <strs.length  ; i++) {
            RoleThrone rt = new RoleThrone();
            rt.setFkRoleId(Integer.parseInt(roleId));
            rt.setFkThroneId(Integer.parseInt(strs[i]));
            int k = mapper.insertSelective(rt);
            if(k>0){
                n = n+1;
            }
        }
        SqlSessionUtil.commit();
        return n>0;
    }

    @Override
    public List<RoleThrone> selectByRoleId(int roleId) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        RoleThroneMapper mapper = sqlSession.getMapper(RoleThroneMapper.class);
        List<RoleThrone> roleThrones = mapper.selectByRoleId(roleId);
        SqlSessionUtil.commit();
        return roleThrones;
    }

}
